# encoding: utf-8
#@author: sydr
#@file: localfixture.py
#@time: 2022/9/20 15:30
import pytest
from pymysql import Connection
from pymysql.cursors import Cursor, DictCursor

from cases.correction.varClass import VarClass
import arrow
import os
from utils.path_generator import filePath


@pytest.fixture(scope="class")
def  deleteBookData(redisConnect,mysqlConnect_sydr):
    yield
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from cus_book where id={}".format(VarClass.bookId)
    curDic.execute(sql)
    sql="delete from cus_book_chapter where id in {};".format(tuple(VarClass.chapterIds))
    curDic.execute(sql)
    sql="delete from cus_book_errata_official where id ={};".format(VarClass.errataId)
    curDic.execute(sql)
    sql="delete from cus_book_errata_user where id={}".format(VarClass.userErrataId)
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def updateBookType (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update cus_book set subject_type=10 where subject_type=6;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update cus_book set subject_type=6 where subject_type=10;"
    curDic.execute(sql)
    con.commit()





@pytest.fixture(scope="function")
def  deleteTimesRecord(redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from tms_user_question_times where user_id={}".format(userId)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def updateTimes (redisConnect,mysqlConnect_sydr,login):
    userId = login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_user_question_times set times=0 where user_id={}".format(userId)
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="class")
def deleteQuestion(redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from tms_question where topic_id={}".format(1319)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def changeStatus (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set from_user_id=-1 where id =2080"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set from_user_id=1111111089 where id =2080"
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="function")
def deleteQuestionTimes (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from tms_user_question_times where user_id=1111111328;"
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def setTimes0 (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_user_question_times set times=0 where user_id=1111111328;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_user_question_times set times=15 where user_id=1111111328;"
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def setUserId (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set from_user_id=1111111328 where id in (218,936,1120,2080);"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set from_user_id=1111111089 where id in (218,936,1120,2080);"
    curDic.execute(sql)
    con.commit()







@pytest.fixture(scope="function")
def setStopicQuestionDelete(redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_stopic_question set is_deleted=1 where id={};".format(3331)
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_stopic_question set is_deleted=0 where id={};".format(3331)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="class")
def deleteQuestionData(redisConnect,mysqlConnect_sydr,login):
    yield
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from tms_question where from_user_id={}".format(userId)
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def setQuestionSubject (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set subject_type=11 where subject_type=4 and id !={}".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set subject_type=4 where id !={} and subject_type=11".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def setOtherReq (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    nowTime = arrow.now().format().split("+")[0]
    sql="update tms_question set oper_user_id={},lock_time='{}' where id={}".format("123456",nowTime,VarClass.questionId)
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set oper_user_id=null,lock_time=null where id={}".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def setManagerType (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set manager_type=2 where id={}".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set manager_type=1 where id={}".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def setQuestionFlow (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="INSERT INTO `tms_question_out_flow` (`cquestion_id`, `user_id`, `type`, `create_time`, `update_time`) VALUES ({}, {}, 1, '2021-05-28 03:45:00', '2021-05-28 03:45:00');".format(VarClass.questionId,userId)
    curDic.execute(sql)
    con.commit()
    yield
    sql="delete from `tms_question_out_flow` where user_id={} and cquestion_id={};".format(userId,VarClass.questionId)
    curDic.execute(sql)
    con.commit()




@pytest.fixture(scope="function")
def  setQuestionAnswered(redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set status=1 where id={}".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set status=0 where id={}".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def changeSubjectType(redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set subject_type=4 where id>=2082 and id<=2084"
    curDic.execute(sql)
    sql="delete from tms_question_out_flow where user_id=1111111328;"
    curDic.execute(sql)

    con.commit()


@pytest.fixture(scope="function")
def subjectType4 (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set subject_type=21 where (id<2082 or id>2084 ) and id !={} and subject_type=4".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set subject_type=4 where (id<2082 or id>2084 ) and id !={} and subject_type=21".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="function")
def delAnswer (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from tms_answer where cquestion_id in (2082,2083,2084,{});".format(VarClass.questionId)
    curDic.execute(sql)
    sql="delete from tms_answer_history where cquestion_id in (2082,2083,2084,{});".format(VarClass.questionId)
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="function")
def delStation (redisConnect,mysqlConnect_sydr,login):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from cms_station_letter_record where user_id={}".format(1111111333)
    curDic.execute(sql)
    con.commit()






@pytest.fixture(scope="class")
def setTimes15 (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_user_question_times set times=15 where user_id=1111111333;"
    curDic.execute(sql)
    con.commit()




@pytest.fixture(scope="function")
def updateAnswer (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from  tms_answer  where cquestion_id={};".format(VarClass.qId_1)
    curDic.execute(sql)
    sql="update tms_answer set cquestion_id={} where id=2080;".format(VarClass.qId_1)
    curDic.execute(sql)
    sql="update tms_question set status=1,lock_time=null where id={};".format(VarClass.qId_1)
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def qaTimesChange (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_qa_config set max_question_num=5 where exam_type=3"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_qa_config set max_question_num=4 where exam_type=3"
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def updateAnswer1 (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="delete from tms_answer where cquestion_id={};".format(VarClass.qId_2)
    curDic.execute(sql)
    sql="update tms_answer set cquestion_id={} where id=2081;".format(VarClass.qId_2)
    curDic.execute(sql)
    sql="update tms_question set status=1,lock_time=null where id={};".format(VarClass.qId_2)
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="function")
def setTimes00 (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_user_question_times set times=0 where user_id=1111111333;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_user_question_times set times=15 where user_id=1111111333;"
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="class")
def initQuestionData (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set subject_type=13 where subject_type=3 and (id<912 or id>925) ;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set subject_type=3 where subject_type=13 and (id<912 or id>925);"
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="class")
def  initNomalQuestion(redisConnect,mysqlConnect_sydr,initQuestionData,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set manager_type=1 where id>=912 and id <=925;"
    curDic.execute(sql)
    sql="update tms_question set oper_user_id={},lock_time='{}' where id>=916 and id<=918;".format(userId,"2022-09-28 14:00:00")
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="class")
def  initAnsweredQuestion(redisConnect,mysqlConnect_sydr,initQuestionData):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set status=1 where id>=912 and id <=925;"
    curDic.execute(sql)
    questionList=[i for i in range(912,926)]
    sql = "delete from tms_answer where cquestion_id in {}".format(tuple(questionList))
    curDic.execute(sql)
    for i in range(6004,6015):
        sql="update tms_answer set cquestion_id={} where id={}".format(questionList[i-6004],i)
        curDic.execute(sql)
    con.commit()





@pytest.fixture(scope="function")
def deleteExcel (redisConnect,mysqlConnect_sydr):
    yield
    path = filePath.casePath + "correction/exportFile.xls"
    os.remove(path)



@pytest.fixture(scope="function")
def outFlow (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set oper_user_id=null where id=2084;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set oper_user_id=1111111328 where id=2084;"
    curDic.execute(sql)
    con.commit()


@pytest.fixture(scope="class")
def changeQuestionTimes (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_user_question_times set times=14 where user_id={} and type=1 and exam_type=3".format(userId)
    curDic.execute(sql)
    sql="INSERT INTO `tms_user_question_times` (`exam_type`, `user_id`, `type`, `times`, `create_time`, `update_time`) VALUES (3, 1111111328, 2, 4, '2022-09-30 14:12:59', '2022-09-30 14:13:01');"
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="class")
def changeQuestionTimes1 (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_user_question_times set times=5 where user_id={} and type=1 and exam_type=3".format(userId)
    curDic.execute(sql)
    sql="update tms_user_question_times set times=3 where user_id={} and type=2 and exam_type=3".format(userId)
    curDic.execute(sql)
    con.commit()





@pytest.fixture(scope="class")
def initQuestionData1 (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set subject_type=33 where subject_type=5 and id<=999 and id>=1024;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_question set subject_type=5 where subject_type=33 and id<=999 and id>=1024;"
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="function")
def  updateForTimeTask(redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_question set oper_user_id=1111111106,status=0,lock_time='2022-09-10 14:00:00' where id in (935,934,933);"
    curDic.execute(sql)
    con.commit()

